package org.dromara.trace.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.trace.domain.vo.TraceShopVo;
import org.dromara.trace.domain.bo.TraceShopBo;
import org.dromara.trace.service.ITraceShopService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 店铺
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/trace/shop")
public class TraceShopController extends BaseController {

    private final ITraceShopService traceShopService;

    /**
     * 查询店铺列表
     */
    @SaCheckPermission("trace:shop:list")
    @GetMapping("/list")
    public TableDataInfo<TraceShopVo> list(TraceShopBo bo, PageQuery pageQuery) {
        return traceShopService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出店铺列表
     */
    @SaCheckPermission("trace:shop:export")
    @Log(title = "店铺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TraceShopBo bo, HttpServletResponse response) {
        List<TraceShopVo> list = traceShopService.queryList(bo);
        ExcelUtil.exportExcel(list, "店铺", TraceShopVo.class, response);
    }

    /**
     * 获取店铺详细信息
     *
     * @param shopId 主键
     */
    @SaCheckPermission("trace:shop:query")
    @GetMapping("/{shopId}")
    public R<TraceShopVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long shopId) {
        return R.ok(traceShopService.queryById(shopId));
    }

    /**
     * 新增店铺
     */
    @SaCheckPermission("trace:shop:add")
    @Log(title = "店铺", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TraceShopBo bo) {
        return toAjax(traceShopService.insertByBo(bo));
    }

    /**
     * 修改店铺
     */
    @SaCheckPermission("trace:shop:edit")
    @Log(title = "店铺", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TraceShopBo bo) {
        return toAjax(traceShopService.updateByBo(bo));
    }

    /**
     * 删除店铺
     *
     * @param shopIds 主键串
     */
    @SaCheckPermission("trace:shop:remove")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shopIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] shopIds) {
        return toAjax(traceShopService.deleteWithValidByIds(List.of(shopIds), true));
    }
}
